package ucas.pin.web.services;

import java.text.Collator;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import ucas.pin.web.domain.Consist;
import ucas.pin.web.domain.Disease;
import ucas.pin.web.domain.MedMaterial;
import ucas.pin.web.domain.Prescription;
import ucas.pin.web.domain.Treat;
import ucas.pin.web.dto.GraphDto;
import ucas.pin.web.dto.LinkDto;
import ucas.pin.web.dto.NodeDto;
import ucas.pin.web.repositories.MedMaterialRepository;
@Service
public class MedMaterialService {
	@Autowired MedMaterialRepository medMaterialRepository;
	
	private GraphDto toEchartsFormat(Collection<MedMaterial> coll_med){	
		List<NodeDto> listNode = new ArrayList();
		List<LinkDto> listLink = new ArrayList();
		for(MedMaterial med : coll_med) {
			System.out.println(med.getName());
			listNode.add(med.toNodeDto(1));
			for(Consist con : med.getConsists()) {
				Prescription pre = (Prescription)con.getStart_Entity();
				listNode.add(pre.toNodeDto(0));
				listLink.add(con.toLinkDto());
			}
		}
		//Collections.sort(listNode,Collator.getInstance(java.util.Locale.CHINA));
		System.out.println("get end");
		return new GraphDto(listNode,listLink);
	}
	
	
	public GraphDto graph(String name) {
		Collection<MedMaterial> result = medMaterialRepository.getGraphByName(name);
		if(result.isEmpty()) {
			result = medMaterialRepository.getEntityByName(name);
		}
		//test_Pre(result);
		System.out.println("get Graph med............"+name);
		
		return toEchartsFormat(result);
	}
	public Collection<MedMaterial> searchByKeyWord(String name) {
		Collection<MedMaterial> result = medMaterialRepository.findMedMaterialByNameLike(".*"+name+".*");
		return result;
	}
	
}
